package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.cri.CriCriticalReport;
import com.ysd.lis.entity.qi.QiIndexItem;
import com.ysd.lis.entity.qi.QiIndexMajor;
import com.ysd.lis.entity.qi.QiIndexSqlDataCollect;
import com.ysd.lis.mapper.qi.QiIndexItemMapper;
import com.ysd.lis.mapper.qi.QiIndexMajorMapper;
import com.ysd.lis.mapper.qi.QiIndexSqlDataCollectMapper;
import com.ysd.lis.request.qi.QiParam;
import com.ysd.lis.service.qi.QiIndexSqlDataCollectService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 指标项目条件表 服务实现类
 * </p>
 *
 * @author PRS
 * @since 2024-07-17
 */
@Service
@Slf4j
public class QiIndexSqlDataCollectServiceImpl extends ServiceImpl<QiIndexSqlDataCollectMapper, QiIndexSqlDataCollect> implements QiIndexSqlDataCollectService {

    @Autowired
    QiIndexSqlDataCollectMapper qiIndexSqlDataCollectMapper;

    @Autowired
    QiIndexItemMapper qiIndexItemMapper;
    @Autowired
    QiIndexMajorMapper qiIndexMajorMapper;

    @Override
    public Result findQiIndexItemAndDataCollectList(QiParam pageParam) {
        String indexId = pageParam.getIndexId();

        //根据指标ID 查出指标下所有的项目
        LambdaQueryWrapper<QiIndexItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QiIndexItem::getIndexId, indexId)
                .eq(QiIndexItem::getDelFlag, 0)
                .orderByAsc(QiIndexItem::getSeq).orderByAsc(QiIndexItem::getItemGroup);
        List<QiIndexItem> qiIndexItemList = qiIndexItemMapper.selectList(queryWrapper);

        //根据指标ID查出指标下所有的分组

        LambdaQueryWrapper<QiIndexMajor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(QiIndexMajor::getDelFlag, 0)
                .eq(QiIndexMajor::getIndexId, indexId)
                .orderByAsc(QiIndexMajor::getSeq);
        List<QiIndexMajor> qiIndexMajorList = qiIndexMajorMapper.selectList(queryWrapper1);

        //根据指标ID查出指标下所有的统计结果
        LambdaQueryWrapper<QiIndexSqlDataCollect> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(QiIndexSqlDataCollect::getDelFlag, 0)
                .eq(QiIndexSqlDataCollect::getIndexId, indexId) ;
        String operateDateStart = pageParam.getOperateDateStart();
        String operateDateEnd = pageParam.getOperateDateEnd();
        if(ToolsUtils.isNotEmpty(operateDateStart) && ToolsUtils.isNotEmpty(operateDateEnd)){
            Timestamp startTime = Timestamp.valueOf(operateDateStart);
            Timestamp endTime = Timestamp.valueOf(operateDateEnd);
            queryWrapper2.ge(QiIndexSqlDataCollect::getOperateDate, startTime);
            queryWrapper2.le(QiIndexSqlDataCollect::getOperateDate, endTime);
        }
        List<QiIndexSqlDataCollect> dataCollectList = qiIndexSqlDataCollectMapper.selectList(queryWrapper2);

        List<Map<String, Object>> returnList = new ArrayList<>();
        //循环项目
        for (QiIndexItem qiIndexItem : qiIndexItemList) {
            Map<String, Object> itemMap = new LinkedHashMap<>();
            itemMap.put("id",qiIndexItem.getId());
            itemMap.put("indexId",qiIndexItem.getIndexId());
            itemMap.put("itemCode",qiIndexItem.getItemCode());
            itemMap.put("itemName",qiIndexItem.getItemName());
            itemMap.put("itemGroup",qiIndexItem.getItemGroup());
            itemMap.put("seq",qiIndexItem.getSeq());
            //循环分组
            for (QiIndexMajor qiIndexMajor : qiIndexMajorList) {
                //找出此项目下分组下的统计结果
                String majorId = qiIndexMajor.getId();
                //找出该分组下的统计结果
                List<QiIndexSqlDataCollect> collect = dataCollectList.stream().filter(col -> col.getMajorId().equals(majorId) && col.getItemId().equals(qiIndexItem.getId())).collect(Collectors.toList());
                if(ToolsUtils.isNotEmpty(collect)){

                    //取collect 的 quantity和
                    int quantity = collect.stream().mapToInt(QiIndexSqlDataCollect::getQuantity).sum();
                    itemMap.put(qiIndexMajor.getMajorCode(),quantity);
                }else {
                    itemMap.put(qiIndexMajor.getMajorCode(),0);
                }
            }

            returnList.add(itemMap);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("returnList",returnList);
        returnMap.put("qiIndexMajorList",qiIndexMajorList);

        return Result.succ(returnMap);

    }
}
